#!/usr/bin/env python
# encoding: utf-8


"""
@file: shanxing.py
@time: 2016/10/20 下午5:14
"""
# 扇形相关知识点
# 面积S 周长C 弧长L 半径R 圆心角A
# 已知两个变量，就能求出其他变量

from mathsolver.functions.root.jiefangchenzu import *
from mathsolver.functions.sanjiao import sanjiao_utils as su

META_VARS = ['S', "C", 'L', 'R', 'A']
META_FORMULAS = {'S': ('L*R/2', 'A*R**2/2'), 'C': ('L+2*R', 'A*R + 2*R'), 'L': ('A*R', 'C - 2*R'),
                 'R': ('L/A', 'C/(2+A)', '(C + sqrt(C**2 - 16))/4'),
                 'A': ('L/R',)}
S, C, L, R, A = META_VARS


#  扇形条件
class SanXingCondition(BaseFunction):
    def solver(self, *args):
        text = args[0]
        value = args[1].value
        if text.find('周长') >= 0:
            self.output.append(BaseSymbolValue({'周长': value}))
        elif text.find('面积') >= 0:
            self.output.append(BaseSymbolValue({'面积': value}))
        elif text.find('圆心角') >= 0:
            self.output.append(BaseSymbolValue({'圆心角': value}))
        return self


class SanXing001(BaseFunction):
    def solver(self, *args):
        self.label.add('扇形弧长公式与面积公式')
        arg0 = args[0]  # eqs
        cond = dict(arg0.sympify())
        values = su.derivation(cond, META_FORMULAS)[0]
        text = args[1]  # 要求的目标
        v_name = ''
        if text.find('面积') >= 0:
            v_name = 'S'
        elif text.find('周长') >= 0:
            v_name = 'C'
        elif text.find('圆形角') >= 0:
            v_name = 'A'
        v_value = values[v_name]
        self.output.append(BaseSymbolValue({v_name: v_value}))
        return self


# 面积S,周长C,弧长L,半径R，圆心角A （弧度值） 5个变量中。已知其中的两个变量的值即可求出其他的变量
class SanXing(BaseFunction):
    CLS = [SanXing001, ]

    @staticmethod
    def known_eqs(known):
        """
        从三角形中搜出关于三角的等式
        :param known:
        :return:
        """
        if not known:
            return None
        shanxing = known['shanxing']
        eqs = []
        # S, C, L, R, A
        if shanxing.S:
            eqs.append(['S', shanxing.S])
        if shanxing.C:
            eqs.append(['C', shanxing.C])
        if shanxing.L:
            eqs.append(['L', shanxing.L])
        if shanxing.R:
            eqs.append(['R', shanxing.R])
        if shanxing.A:
            eqs.append(['A', shanxing.A])
        return BaseEqs(eqs)

    def solver(self, *args):
        r = None
        eqs = SanXing.known_eqs(self.known)
        temp_args = [eqs, ]
        temp_args.extend([arg for arg in args])
        for cl in SanXing.CLS:
            try:
                r = cl(verbose=True).solver(*temp_args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r


if __name__ == '__main__':
    pass
